
package com.huizhan.web.controller;
 
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.*;
import java.util.*;

import com.huizhan.web.dao.*;
import com.huizhan.web.model.*;

/**
 * 注册属性api
 */
 
@Api(tags = "Property",description = "注册属性")
@RestController
public class PropertyController {
    
    @Autowired
    private PropertyDao dao;
   
    /**
     * 获取注册属性集合
     * @return api结果
     */
    @ApiOperation(value = "获取注册属性集合")
    @RequestMapping(value =  "/api/Property/",method = {RequestMethod.GET})
    public ApiResult<List<Property>> Get(){
         ApiResult<List<Property>> result=new ApiResult<List<Property>>();
        try 
        {
            List<Property> res = dao.findAll();
            result.setData(res);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据注册ID获取属性集合
     * @param id 注册ID
     * @return api结果
     */
    @ApiOperation(value = "根据注册ID获取属性集合")
    @RequestMapping(value =  "/api/Property/register/{registerId}",method = {RequestMethod.GET})
    public ApiResult<List<Property>> Project(@PathVariable Long registerId){
        ApiResult<List<Property>> result=new ApiResult<List<Property>>();
        try
        {
            List<Property> model= dao.findByRegisterId(registerId);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据ID获取注册属性信息
     * @param id 注册属性ID
     * @return api结果
     */
    @ApiOperation(value = "根据ID获取注册属性信息")
    @RequestMapping(value =  "/api/Property/{id}",method = {RequestMethod.GET})
    public ApiResult<Property> Get(@PathVariable Long id){
        ApiResult<Property> result=new ApiResult<Property>();
        try
        {
            Property model= dao.getOne(id);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 修改注册属性信息
     * @param id 注册属性ID
     * @param model 注册属性信息
     * @return api结果
     */
    @ApiOperation(value = "修改注册属性信息")
    @RequestMapping(value =  "/api/Property/{id}",method = {RequestMethod.PUT})
    public ApiResult<Object> Edit(@PathVariable Long id,@RequestBody List<Property> model){
        ApiResult<Object> result=new ApiResult<Object>();
        try
        {
            dao.deleteByRegisterId(id);
            for (Property property : model) {
                property.setRegisterId(id);
                dao.save(property);
            }
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 添加注册属性信息
     * @param model 注册属性信息
     * @return api结果
     */
    @ApiOperation(value = "添加注册属性信息")
    @RequestMapping(value =  "/api/Property/",method = {RequestMethod.POST})
    public ApiResult<Object> Add(@PathVariable Long id,@RequestBody List<Property> model){
         ApiResult<Object> result=new ApiResult<Object>();
        try
        {
            for (Property property : model) {
                property.setCreateDate(new Date());
                dao.save(property);
            }
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除注册属性信息
     * @param id 注册属性ID
     * @return api结果
     */
    @ApiOperation(value = "删除注册属性信息")
    @RequestMapping(value =  "/api/Property/{id}",method = {RequestMethod.DELETE})
    public ApiResult<Object> Delete(@PathVariable  Long id){
        ApiResult<Object> result=new ApiResult<Object>();
        try 
        {
            dao.deleteById(id);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}

